class Demo:
    def __init__(self):
        self.data = [('张三', 1), ('李四', 2), ('王五', 3)]
        self.counter = 0

    def __len__(self):
        print('数据的长度为：', end='')
        return self.data.__len__()

    def __getitem__(self, index):
        print(f'正在获取了索引{index}位置的元素')
        return self.data[index]

    def __delitem__(self, key):
        result = [x for x in self.data if x[0] == key]
        if len(result) == 0:
            print('没有对应的数据')
            return
        print('已删除数据')
        self.data.remove(result[0])

    def __setitem__(self, key, value):
        result = [x for x in self.data if x == (key, value)]
        if len(result) != 0:
            print('存在重复的数据')
            return
        print('数据已添加')
        self.data.append((key, value))

    def __contains__(self, key):
        result = [x for x in self.data if x == key]
        if len(result) == 0:
            print('没有匹配的数据')
            return False
        print('存在匹配的数据')
        return True

    def __iter__(self):
        return self

    def __next__(self):
        current = self.counter
        if self.counter >= self.data.__len__():
            raise StopIteration
        self.counter += 1
        return self.data[current]


if __name__ == '__main__':
    demo = Demo()
    print(len(demo))
    print('*'*100)
    print(demo[0])
    print('*' * 100)
    del demo['张三']
    print('*' * 100)
    demo['张三'] = 4
    print('*' * 100)
    print(('李四', 2) in demo)
    print('*' * 100)
    for i in demo:
        print(i)
